﻿

(function () {
    appModule.controller('common.views.specialPersons.createOrEditModal', [
        '$scope', '$uibModalInstance', 'abp.services.app.specialPerson', 'specialPersonId', 'specialPerson', 'abp.services.app.area', 'abp.services.app.personType', 'personTypeID', 'abp.services.app.user',
        function ($scope, $uibModalInstance, specialPersonService, specialPersonId, specialPerson, areaService, personTypeService, personTypeID, userService) {
            var vm = this;
            vm.saving = false;
            //首先将specialPerson数据设置为null
            vm.specialPerson = null;



            //触发保存方法
            vm.save = function () {
                vm.saving = true;
                specialPersonService.createOrUpdateSpecialPersonAsync({ specialPersonEditDto: vm.specialPerson }).then(function () {
                    abp.notify.info(app.localize('SavedSuccessfully'));
                    $uibModalInstance.close();
                }).finally(function () {
                    vm.saving = false;
                });


            };
            //取消关闭页面
            vm.cancel = function () {
                $uibModalInstance.dismiss();
            };

            //初始化页面
            function init() {

                //绑定性别
                var sexData = { data: [{ 'name': '男', 'value': true }, { 'name': '女', 'value': false }] };
                vm.sexList = sexData.data;

                //绑定人员类别控件
                personTypeService.getAllPersonTypeListAsync().then(function (result) {
                    vm.personTypeList = result.data.items;
                });

                //绑定文化程度
                specialPersonService.getAllSpeCultureList().then(function (result) {
                    vm.disCultureList = result.data;
                });

                //绑定健康状况
                specialPersonService.getAllSpeHealthList().then(function (result) {
                    vm.disHealthList = result.data;
                });

                //绑定民族
                specialPersonService.getAllSpeNationList().then(function (result) {
                    vm.disNationList = result.data;
                });

                //绑定政治面貌
                specialPersonService.getAllSpePoliticsList().then(function (result) {
                    vm.disPoliticsList = result.data;
                });


                var b = specialPersonId != null;

                var townId = '';
                var areaId = '';
                var currUser;

                specialPersonService.getSpecialPersonForEditAsync({
                    id: specialPersonId
                }).then(function (result) {
                    if (b) {
                        vm.specialPerson = result.data.specialPerson;
                        vm.specialPerson.personTypeID = personTypeID;
                        currUser = result.data.specialPerson;
                        townId = currUser.townID;
                        areaId = currUser.areaID;
                        InitAreas();
                    } else {
                        vm.specialPerson = result.data.specialPerson;
                        vm.specialPerson.personTypeID = personTypeID;

                        //获取Session中的userId
                        var userid = abp.session.userId;
                        //获取用户数据
                        userService.getUsersGyUserId({ userid: userid }).then(function (result) {
                            currUser = result.data;
                            townId = currUser.townID;
                            areaId = currUser.areaID;
                            InitAreas();
                        });
                    }
                });

                function InitAreas() {
                    //绑定镇控件
                    areaService.getAreaByLevelAsync({ id: 0 }).then(function (result) {
                        vm.townList = result.data.items;
                        vm.specialPerson.townID = currUser.townID.toString();
                    });
                    //绑定区控件
                    areaService.getAreaByLevelAsync({ id: townId }).then(function (result) {
                        vm.areaList = result.data.items;
                        vm.specialPerson.areaID = currUser.areaID.toString();
                    });
                    //绑定村控件
                    areaService.getAreaByLevelAsync({ id: areaId }).then(function (result) {
                        vm.villageList = result.data.items;
                        vm.specialPerson.villageID = currUser.villageID.toString();
                    });
                }
            }

            //绑定区
            vm.bindArea = function (townID) {
                areaService.getAreaByLevelAsync({ id: townID }).then(function (result) {
                    vm.areaList = result.data.items;
                });
            };

            //绑定村
            vm.bindVillage = function (areaID) {
                areaService.getAreaByLevelAsync({ id: areaID }).then(function (result) {
                    vm.villageList = result.data.items;
                });
            };

            //执行初始化方法
            init();
        }
    ]);
})();